package com.gzyj.server.factory.jtt808;

import com.gzyj.jtt808.v2013.enums.BusinessDataType;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.down.*;
import com.gzyj.server.factory.jtt808.up.*;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lhj
 */
@Component
public class Common808HandlerFactory implements ApplicationContextAware {
    private static Map<Integer, CommonHandler> handlers = new ConcurrentHashMap<>();
    private static ApplicationContext context;


    @PostConstruct
    public void serverHandlerFactory() {
        {//上行
            handlers.put(BusinessDataType.终端通用应答.getCode(), context.getBean(JTT808_0x0001_Handler.class));
            handlers.put(BusinessDataType.终端心跳.getCode(), context.getBean(JTT808_0x0002_Handler.class));
            handlers.put(BusinessDataType.终端注册.getCode(), context.getBean(JTT808_0x0100_Handler.class));
            handlers.put(BusinessDataType.终端鉴权.getCode(), context.getBean(JTT808_0x0102_Handler.class));
            handlers.put(BusinessDataType.查询终端属性应答.getCode(), context.getBean(JTT808_0x0107_Handler.class));
            handlers.put(BusinessDataType.位置信息汇报.getCode(), context.getBean(JTT808_0x0200_Handler.class));
            handlers.put(BusinessDataType.驾驶员身份信息采集上报.getCode(), context.getBean(JTT808_0x0702_Handler.class));
            handlers.put(BusinessDataType.终端上传音视频资源列表.getCode(), context.getBean(JTT808_0x1205_Handler.class));
            handlers.put(BusinessDataType.文件上传完成通知.getCode(), context.getBean(JTT808_0x1206_Handler.class));
            handlers.put(BusinessDataType.查询终端参数应答.getCode(), context.getBean(JTT808_0x0104_Handler.class));

        }

        {
            handlers.put(BusinessDataType.实时音视频传输请求.getCode(), context.getBean(JTT808_0x9101_Handler.class));
            handlers.put(BusinessDataType.音视频实时传输控制.getCode(), context.getBean(JTT808_0x9102_Handler.class));
            handlers.put(BusinessDataType.查询资源列表.getCode(), context.getBean(JTT808_0x9205_Handler.class));
            handlers.put(BusinessDataType.文件上传指令.getCode(), context.getBean(JTT808_0x9206_Handler.class));
            handlers.put(BusinessDataType.查询终端属性.getCode(), context.getBean(JTT808_0x8107_Handler.class));
            handlers.put(BusinessDataType.查询指定终端参数.getCode(), context.getBean(JTT808_0x8106_Handler.class));
            handlers.put(BusinessDataType.设置终端参数.getCode(), context.getBean(JTT808_0x8103_Handler.class));
        }

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (null == context) {
            context = applicationContext;
        }
    }

    public CommonHandler getHandler(Integer flag) {
        return handlers.get(flag);
    }

}
